﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Coffee_Restaurant.DAL;
using Coffee_Restaurant.Database;
using Coffee_Restaurant.Models;
using log4net;
using MetroFramework;
using MetroFramework.Forms;

namespace Coffee_Restaurant.Forms.DinnerMenus
{
    public partial class AddDinnerMenu : MetroForm
    {
        ILog log = log4net.LogManager.GetLogger(typeof(AddDinnerMenu));
        //event edit
        public delegate void ButtonSaveEventHandler();
        public event ButtonSaveEventHandler BtnSave;

        public AddDinnerMenu()
        {
            InitializeComponent();

        }

        private void AddItem_Load(object sender, EventArgs e)
        {
            try
            {
                //load style default
                var style = AppSettingDa.GetValue("Style");
                if (style != null)
                {
                    this.Style = (MetroColorStyle)(int.Parse(style));
                }
                this.dinnerMenuCategoriesTableAdapter.Fill(this.coffeeRestaurantDataSet.DinnerMenuCategories);
                this.itemUnitsTableAdapter.Fill(this.coffeeRestaurantDataSet.ItemUnits);
            }
            catch (Exception ex)
            {
                LogError(ex);
            }
        }

        //btn dong y
        private void metroButton1_Click(object sender, EventArgs e)
        {
            if (ValidateForm())
            {
                try
                {
                    using (var db = new DatabaseEntities())
                    {
                        //add to database
                        var guid = Guid.NewGuid().ToString();
                        db.DinnerMenus.Add(new DinnerMenu()
                        {
                            Name = txtName.Text,
                            Code = txtCode.Text,
                            Images = _desfilepath,
                            Unit = cbbDVT.Text,
                            Price = ValidateHelper.GetValueTryParseDecimal(txtPrice.Text),
                            Price2 = ValidateHelper.GetValueTryParseDecimal(txtPrice2.Text),
                            Price3 = ValidateHelper.GetValueTryParseDecimal(txtPrice3.Text),
                            Datetime = DateTime.Now,
                            GuiId = guid
                        });
                        if (db.SaveChanges() > 0)
                        {
                            var model = db.DinnerMenus.FirstOrDefault(a => a.GuiId == guid);
                            if (model != null)
                            {
                                BtnSave();
                            }
                            this.Hide();
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogError(ex);
                }
            }

        }

        bool ValidateForm()
        {
            if (!ValidateHelper.IsString(txtName.Text, 2))
            {
                txtName.Focus();
                return false;
            }
            if (!ValidateHelper.IsString(cbbDVT.Text, 2))
            {
                cbbDVT.Focus();
                return false;
            }
            if (!ValidateHelper.IsString(cbbCategory.Text, 2))
            {
                cbbCategory.Focus();
                return false;
            }
            if (!ValidateHelper.IsNumberOrDecimals(txtPrice.Text, 4))
            {
                txtPrice.Focus();
                return false;
            }
            //neu don gia 2 duoc nhap thi se validate
            if (ValidateHelper.CountLength(txtPrice2.Text) > 4)
            {
                if (!ValidateHelper.IsNumberOrDecimals(txtPrice2.Text, 4))
                {
                    txtPrice2.Focus();
                    return false;
                }
            }
            //neu don gia 3 duoc nhap thi se validate
            if (ValidateHelper.CountLength(txtPrice3.Text) > 4)
            {
                if (!ValidateHelper.IsNumberOrDecimals(txtPrice3.Text, 4))
                {
                    txtPrice3.Focus();
                    return false;
                }
            }
            return true;
        }

        private void metroButton2_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void txtPrice_TextChanged(object sender, EventArgs e)
        {
            lblPriceFormat.Text = ValidateHelper.TryParseDecimal(txtPrice.Text);
        }
        private void txtPrice2_TextChanged(object sender, EventArgs e)
        {
            lblPriceFormat2.Text = ValidateHelper.TryParseDecimal(txtPrice2.Text);
        }
        private void txtPrice3_TextChanged(object sender, EventArgs e)
        {
            lblPriceFormat3.Text = ValidateHelper.TryParseDecimal(txtPrice3.Text);
        }

        private string _desfilepath;
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            try
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    var filepath = openFileDialog1.FileName;
                    var fileInfo = new FileInfo(filepath);
                    var filename = fileInfo.Name;
                    var dic = AppDomain.CurrentDomain.BaseDirectory;
                    //neu chua co folder thi tao folder
                    dic += "\\anhthucdon\\";
                    if (!Directory.Exists(dic))
                    {
                        Directory.CreateDirectory(dic);
                    }
                    _desfilepath = dic + filename;
                    //kiem tra xem file da co chua
                    if (!File.Exists(_desfilepath))
                    {
                        File.Copy(filepath, _desfilepath);
                    }
                    else
                    {
                        pictureBox1.Image = Image.FromFile(_desfilepath);
                    }
                }
            }
            catch (Exception ex)
            {
                LogError(ex);
            }
        }

        public void LogError(Exception ex)
        {
            CustomMessageBox.Show(ex.Message, "Có lỗi xảy ra", "alert", MetroColorStyle.Red);
            int line = (new StackTrace(ex, true)).GetFrame(0).GetFileLineNumber();
            log.Error(ex.Message + " Line: " + line.ToString());
        }
    }
}
